[id].vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <main>
  3. <div>
  4. <div v-if="pending">
  5. Pending...
  6. </div>
  7. <div v-else>
  8. <h1>{{ $t('download') }}</h1>
  9. <p>Edit :{{ file }}</p>
  10. <UiForm v-if="!pending" :model="File" :entity="file">
  11. <template #form.input="{model, entity: file}">
  12. <UiInputText field="name" v-model="file.name" type="text" :rules="rules.nameRules" />
  13. <UiInputEnum field="status" v-model="file.status" enum="file_status" />
  14. <v-btn @click="cancel" class="ma-5">Annuler</v-btn>
  15. <v-btn @click="save" class="ma-5">Enregistrer</v-btn>
  16. <v-btn @click="deleteAndGoBack" class="ma-5">Supprimer</v-btn>
  17. <v-btn @click="refresh" class="ma-5">Refresh</v-btn>
  18. <v-btn @click="goBack" class="ma-5">Retour</v-btn>
  19. <v-btn to="/poc/display" class="ma-5">Détails</v-btn>
  20. </template>
  21. </UiForm>
  22. <v-btn to="/poc">Retour</v-btn>
  23. </div>
  24. </div>
  25. </main>
  26. </template>
  27. <script setup lang="ts">
  28. import {useEntityManager} from "~/composables/data/useEntityManager";
  29. import {ref} from "@vue/reactivity";
  30. import type {Ref} from "@vue/reactivity";
  31. import File from "~/models/Core/File";
  32. import {useEntityFetch} from "~/composables/data/useEntityFetch";
  33. import {useI18n} from "vue-i18n";
  34. const route = useRoute()
  35. const id: Ref<number> = ref(parseInt(route.params.id as string))
  36. const { em }= useEntityManager()
  37. const { fetch } = useEntityFetch()
  38. const { data: file, pending, refresh } = fetch(File, id.value)
  39. // Get file from store
  40. // const file: ComputedRef<File> = computed( () => {
  41. // return em.find(File, id.value)
  42. // })
  43. // Update store when form is changed
  44. // const onFileChange = () => {
  45. // console.log(file.value)
  46. // em.save(File, file.value)
  47. // }
  48. const i18n = useI18n()
  49. const rules = {
  50. nameRules: [
  51. (nameValue: string) => !!nameValue || i18n.t('required'),
  52. (nameValue: string) => (nameValue || '').length <= 128 || i18n.t('name_length_rule'),
  53. (nameValue: string) => (nameValue || '').length >= 2 || i18n.t('name_length_rule'),
  54. ]
  55. }
  56. const save = async () => {
  57. if (file.value === null) {
  58. throw new Error('File not found')
  59. }
  60. await em.persist(File, file.value)
  61. }
  62. const cancel = async () => {
  63. if (file.value === null) {
  64. throw new Error('File not found')
  65. }
  66. if (em.isNewInstance(File, id.value)) {
  67. await em.delete(File, file.value)
  68. } else {
  69. em.reset(File, file.value)
  70. }
  71. }
  72. const deleteItem = async () => {
  73. if (file.value === null) {
  74. throw new Error('File not found')
  75. }
  76. await em.delete(File, file.value)
  77. }
  78. const goBack = () => {
  79. navigateTo('/poc')
  80. }
  81. const saveAndGoBack = async () => {
  82. await save()
  83. goBack()
  84. }
  85. const cancelAndGoBack = async () => {
  86. await cancel()
  87. goBack()
  88. }
  89. const deleteAndGoBack = async () => {
  90. await deleteItem()
  91. goBack()
  92. }
  93. </script>
  94. <style>
  95. </style>